//城市选择
var app = getApp();
Page({
  data: {
    array: [],
     index:0,
    location: [],
    province: '请选择省',
    city: '请选择城市',
    district: '请选择地区',
    street:'请输入详细地址',
    privinces:[],
    privincesId:[],
    privincesIndex: 0,

    citys:[],
    citysId:[],
    citysIndex: 0,

    districts:[],
    districtsId: [],
    districtsIndex: [],
    street_number:'',
    address: '',

    shengArr: [],//省级数组
    shengId: [],//省级id数组
    shiArr: [],//城市数组
    shiId: [],//城市id数组
    quArr: [],//区数组
    shengIndex: 0,
    shiIndex: 0,
    quIndex: 0,
    mid: 0,
    sheng: 0,
    city: 0,
    area: 0,
    code: 0,
    cartId: 0
  },
  bindPrivinceChange: function (e) {
    var that=this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var pro = this.data.privinces[e.detail.value]
    var id = this.data.privincesId[e.detail.value]
    that.setData({
      province: pro
    })
    wx.request({
      url: app.d.ceshiUrl + '/user/AreaMgrAction!listChilds?area.id='+id,
      data: {},
      method: 'POST',
      success: function (res) {
        console.log("获取城市")
        console.log(res)
        var status = res.data.status;
        var city = res.data.rows;
        var sArr = [];
        var sId = [];
        sArr.push('请选择');
        sId.push('0');
        for (var i = 0; i < city.length; i++) {
          
          sArr.push(city[i].areaName);
          sId.push(city[i].id);
        }
        that.setData({
          citys: sArr,
          citysId: sId
        })
      },
      fail: function () {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },

  bindCityChange:function(e){
    var that=this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var pro = this.data.citys[e.detail.value]
    var id = this.data.citysId[e.detail.value]
    this.setData({
      city: pro
    })

    wx.request({
      url: app.d.ceshiUrl + '/user/AreaMgrAction!listChilds?area.id=' + id,
      data: {},
      method: 'POST',
      success: function (res) {
        console.log("获取地区")
        console.log(res)
        var status = res.data.status;
        var districts = res.data.rows;
        var sArr = [];
        var sId = [];
        sArr.push('请选择');
        sId.push('0');
        for (var i = 0; i < districts.length; i++) {
          sArr.push(districts[i].areaName);
          sId.push(districts[i].id);
        }
        that.setData({
          districts: sArr,
          districtsId: sId
        })
      },
      fail: function () {
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })
  },
  bindDistrictChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var pro = this.data.districts[e.detail.value]
    this.setData({
      district: pro
    })
  },

  formSubmit: function (e) {
    var adds = e.detail.value;
    var cartId = this.data.cartId;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Address/add_adds',
      data: {
        user_id:app.d.userId,
        receiver: adds.name,
        tel: adds.phone,
        sheng: this.data.sheng,
        city: this.data.city,
        quyu: this.data.area,
        adds: adds.address,
        code: this.data.code,
      },
      method: 'POST',  
      header: { 
        'Content-Type':  'application/x-www-form-urlencoded'
      },
      success: function (res) {
        // success
        var status = res.data.status;
        if(status==1){
          wx.showToast({
            title: '保存成功！',
            duration: 2000
          });
        }else{
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
        }
        wx.redirectTo({
          url: 'user-address/user-address?cartId=' + cartId
        });
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    })


  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this;
   

    var location = wx.getStorageSync('location')
    var province = wx.getStorageSync('province')
    var city = wx.getStorageSync('city')
    var district = wx.getStorageSync('district')   
    var street = wx.getStorageSync('street')
    var street_number = wx.getStorageSync('street_number')

    var address = wx.getStorageSync('address')
    
    that.setData({
      cartId: options.cartId,
      location: location,
      province: province,
      city: city,
      district: district,
      street: street,
      street_number: street_number,
      address: address,

    })

    //获取省级城市
    wx.request({
      url: app.d.ceshiUrl + '/user/AreaMgrAction!listByLevel?area.level=0',
      data: {},
      method: 'POST',
      success: function (res) {
        console.log("获取省级城市")
        console.log(res)
        var status = res.data.status;
        var province = res.data.rows;
        var sArr = [];
        var sId = [];
        sArr.push('请选择');
        sId.push('0');
        for (var i = 0; i < province.length; i++) {
          sArr.push(province[i].areaName);
          sId.push(province[i].id);
        }
        that.setData({
 
          privinces: sArr,
          privincesId: sId
        })
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      },
    })

  },

  // bindPickerChangeshengArr: function (e) {
  //   this.setData({
  //     shengIndex: e.detail.value,
  //     shiArr: [],
  //     shiId: [],
  //     quArr:[],
  //     quiId: []
  //   });
  //   var that = this;
  //   wx.request({
  //     url: app.d.ceshiUrl + '/Api/Address/get_city',
  //     data: {sheng:e.detail.value},
  //     method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  //     header: {// 设置请求的 header
  //       'Content-Type':  'application/x-www-form-urlencoded'
  //     },
  //     success: function (res) {
  //       // success
  //       var status = res.data.status;
  //       var city = res.data.city_list;

  //       var hArr = [];
  //       var hId = [];
  //       hArr.push('请选择');
  //       hId.push('0');
  //       for (var i = 0; i < city.length; i++) {
  //         hArr.push(city[i].name);
  //         hId.push(city[i].id);
  //       }
  //       that.setData({
  //         sheng:res.data.sheng,
  //         shiArr: hArr,
  //         shiId: hId
  //       })
  //     },
  //     fail: function () {
  //       // fail
  //       wx.showToast({
  //         title: '网络异常！',
  //         duration: 2000
  //       });
  //     },

  //   })
  // },
  // bindPickerChangeshiArr: function (e) {
  //   this.setData({
  //     shiIndex: e.detail.value,
  //     quArr:[],
  //     quiId: []
  //   })
  //   var that = this;
  //   wx.request({
  //     url: app.d.ceshiUrl + '/Api/Address/get_area',
  //     data: {
  //       city:e.detail.value,
  //       sheng:this.data.sheng
  //     },
  //     method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  //     header: {// 设置请求的 header
  //       'Content-Type':  'application/x-www-form-urlencoded'
  //     },
  //     success: function (res) {
  //       var status = res.data.status;
  //       var area = res.data.area_list;

  //       var qArr = [];
  //       var qId = [];
  //       qArr.push('请选择');
  //       qId.push('0');
  //       for (var i = 0; i < area.length; i++) {
  //         qArr.push(area[i].name)
  //         qId.push(area[i].id)
  //       }
  //       that.setData({
  //         city:res.data.city,
  //         quArr: qArr,
  //         quiId: qId
  //       })
  //     },
  //     fail: function () {
  //       // fail
  //       wx.showToast({
  //         title: '网络异常！',
  //         duration: 2000
  //       });
  //     }
  //   })
  // },
  // bindPickerChangequArr: function (e) {
  //   console.log(this.data.city)
  //   this.setData({
  //     quIndex: e.detail.value
  //   });
  //   var that = this;
  //   wx.request({
  //     url: app.d.ceshiUrl + '/Api/Address/get_code',
  //     data: {
  //       quyu:e.detail.value,
  //       city:this.data.city
  //     },
  //     method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  //     header: {// 设置请求的 header
  //       'Content-Type':  'application/x-www-form-urlencoded'
  //     },
  //     success: function (res) {
  //       that.setData({
  //         area:res.data.area,
  //         code:res.data.code
  //       })
  //     },
  //     fail: function () {
  //       // fail
  //       wx.showToast({
  //         title: '网络异常！',
  //         duration: 2000
  //       });
  //     }
  //   })
  // }

})